home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / MFWM / INIT.c next >
C/C++ Source or Header  |  1997-09-24  |  2KB  |  91 lines

  1. /*
  2. *
  3. *    INIT.c
  4. *
  5. *    Rob Hagopian
  6. *    with Oleg Kiselyov
  7. *    Based on code by: Joe Zobkiw
  8. *
  9. */
  10.  
  11. /* #include files */
  12.  
  13. #include "A4Stuff.h"
  14. #include "SetupA4.h"
  15.  
  16. /* #defines */
  17.  
  18. #define kINITid            0
  19.  
  20. /* FrontWindow patch stuff */
  21.  
  22. typedef pascal WindowPtr (*FrontWinProc)(void);
  23. FrontWinProc    gOldFrontWinAddr;
  24. pascal WindowPtr FrontWinPatch(void);
  25.  
  26. /* main */
  27.  
  28. void main(void)
  29. {
  30.     long    oldA4;
  31.     Handle    h;
  32.     
  33.     /* set up our A4 context for _this file_ */
  34.     oldA4 = SetCurrentA4();
  35.     RememberA4();
  36.     
  37.     /* detach ourselves */
  38.     h = Get1Resource('INIT', kINITid);
  39.     if (h) DetachResource(h);
  40.     
  41.     /* initialize the globals in _this file_ */
  42.     gOldFrontWinAddr = 0L;
  43.     
  44.     gOldFrontWinAddr = (FrontWinProc)GetToolTrapAddress(_FrontWindow);
  45.     SetToolTrapAddress((UniversalProcPtr)FrontWinPatch, _FrontWindow);
  46.     
  47.     /* restore the a4 world */
  48.     SetA4(oldA4);
  49. }
  50.  
  51. // Starting from win, find a window whose structure ("content")
  52. // region contain the point pt (in abs coordinates)
  53. // Note! Make sure it supports b/w windows, too
  54. static CWindowPeek which_window_mouse_is_in(Point pt, CWindowPeek win)
  55. {
  56.     for(; win; win = win->nextWindow) {
  57.         // skip over empty or invisible windows
  58.         if( win->contRgn == nil || !win->visible ) continue;
  59.         if(PtInRgn(pt,win->strucRgn))
  60.           return PtInRgn(pt,win->contRgn) ? win : nil;
  61.     }
  62.     return nil;
  63. }
  64.  
  65.  
  66. pascal WindowPtr FrontWinPatch(void)
  67. {
  68.     WindowPtr                thewin;
  69.     char *                     the_a5=NULL;
  70.     
  71. /* use the global data in _this file_ */
  72.     const long oldA4 = SetUpA4();
  73.  
  74.     thewin = gOldFrontWinAddr();
  75.         /* restore the a4 world */
  76.         RestoreA4(oldA4);
  77.     if( thewin == nil )
  78.        return nil;
  79.     if( ((CWindowPeek)thewin)->windowKind == dialogKind )
  80.       return thewin;
  81.  
  82.     {
  83.         EventRecord the_event;
  84.         WindowPtr another_win;
  85.         OSEventAvail(0, &the_event);
  86.         another_win = (WindowPtr)which_window_mouse_is_in(the_event.where, (CWindowPeek)thewin);
  87.         return another_win ? another_win : thewin;
  88.     }
  89. }
  90.  
  91.